package com.lam.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lam.dormitory.entity.Student;
import com.lam.dormitory.service.IDormitoryService;
import com.lam.dormitory.service.IRoleService;
import com.lam.dormitory.service.IStudentService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.StudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Api(tags = "学生接口")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDormitoryService dormitoryService;

    @ApiOperation(value = "学生详情", notes = "根据用户userId学生详情")
    @RequiresRoles(value = {"admin", "superadmin","student"}, logical = Logical.OR)
    @PostMapping("/detail")
    public ResponseTemplate stuDetailByUserId(Long userId) {
        Student student = new Student();
        student.setUserId(userId);
        List<StudentVo> studentList = studentService.selectByStudent(student);
        return new ResponseTemplate(200, "学生详情返回成功", studentList);
    }

    /*编辑学生*/
    @ApiOperation(value = "编辑学生", notes = "编辑学生")
    @PostMapping("/edit")
//    @RequiresPermissions("role:edit")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate editStudent(Student student) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Student::getUserId, student.getUserId());
        boolean b = studentService.update(student, wrapper);
        if (b) {
            return new ResponseTemplate(200, "编辑成功");
        } else {
            return new ResponseTemplate(500, "编辑失败");
        }
    }


    @ApiOperation(value = "学生列表数据", notes = "可根据stuNo,stuName,stuAge,stuSex,stuMajor,stuPhone，dorNo,buildingNo查询学生列表数据,pageNum 默认为 1，pageSize默认为 0")
    @PostMapping("/list")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate pageStudents(Student student, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudentVo> studentList = studentService.selectByStudent(student);
        PageInfo<StudentVo> pageInfo = new PageInfo<>(studentList);
        return new ResponseTemplate(200, "获取学生列表数据成功", studentList, pageInfo.getTotal());
    }

    @ApiOperation(value = "删除学生", notes = "根据userId删除学生")
    @RequiresRoles(value = {"superadmin", "admin"}, logical = Logical.OR)
//    @RequiresPermissions("user:delete")
    @PostMapping("/delete")
    public ResponseTemplate deleteStudent(Long userId) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Student::getUserId, userId);
        boolean result = studentService.remove(wrapper);
        if (result) {
            return new ResponseTemplate(200, "学生删除成功");
        }
        return new ResponseTemplate(500, "学生删除失败");
    }

    @ApiOperation(value = "学生展示列表", notes = "查询所有学生和已被分配宿舍的学生")
    @PostMapping("/showList")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate selectStudent(){
        Map<String,Object> map  = new HashMap<>();
        List<Student> allStudent = studentService.list();
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("dor_id").notIn("dor_id",0);
        List<Student> showStudent = studentService.list(wrapper);
        List<Student> major = studentService.selectMajor();
        map.put("allStudent",allStudent);
        map.put("showStudent",showStudent);
        map.put("majorList",major);
        return new ResponseTemplate(200,"查询成功",map);
    }

    @ApiOperation(value = "添加学生", notes = "添加学生")
    @PostMapping("/add")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate addStudent(@RequestParam(value = "userIds") List<Long> userIds){
        for (Long userId : userIds) {
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Student::getUserId, userId).eq(Student::getStatus,1);
            studentService.update(wrapper);
        }
        return new ResponseTemplate(200,"添加成功");
    }

    @ApiOperation(value = "查询学生无重复专业年级班级", notes = "查询学生无重复专业年级班级")
    @PostMapping("/majorGradeClassList")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate majorGradeClassList(){
        Map<String,Object> map  = new HashMap<>();
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("stu_major");
        List<Student> majorList = studentService.list(queryWrapper);

        QueryWrapper<Student> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.groupBy("stu_grade");
        List<Student> gradeList = studentService.list(queryWrapper2);

        QueryWrapper<Student> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.groupBy("stu_class");
        List<Student> classList = studentService.list(queryWrapper3);
        map.put("majorList",majorList);
        map.put("gradeList",gradeList);
        map.put("classList",classList);
        return new ResponseTemplate(200,"查询成功",map);
    }
}
